from storage_mysql import Todomysql
from flask import Flask, request

app = Flask(__name__)

# 对中文编码 JSON 格式时，使用原始中文
app.config['JSON_AS_ASCII'] = False

storage = Todomysql()
storage.get_connec()
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"


@app.route("/todos", methods=['GET'])
def list_todos():
    return {"todos": [x.__dict__ for x in storage.get_list()]}


@app.route("/todos", methods=['POST'])
def create_todo():
    if 'title' not in request.form:
        return '参数错误，必须提供 title 参数', 400
    todo = storage.create(request.form['title'])
    return todo.__dict__


@app.route("/todos/mark_completed", methods=['POST'])
def mark_completed():
    if 'todo_id' not in request.form:
        return '参数错误，必须提供 todo_id 参数', 400
    todo_id = request.form['todo_id']
    obj=storage.mark_completed(todo_id)
    return obj
if __name__ == '__main__':
    app.run()